package com.android.hedgehog.puzzles;

import java.util.Random;

import com.android.hedgehog.Assets;
import com.android.hedgehog.Settings;

public class TimeBombField {
	public String[] colors = new String[] { "RED", "BLUE", "GREEN", "YELLOW", "BLACK", "WHITE", "BROWN",
			"PURPLE" };
	public int[] wireOrder;
	public int nextWireToCut = 0;
	public int numberOfWires = 14;
	public Wire[] wires;
	public int bombTime;
	public double onTime, offTime;
	public int mysteryPosition;
	Random rand = new Random();

	public TimeBombField(int difficulty) {
		setDifficulty(difficulty);
		instantiate();

	}

	private void setDifficulty(int difficulty) {

		onTime = 1 - (difficulty * .1);
		offTime = 1 + (difficulty * .1);
		mysteryPosition = 14 - difficulty;
		bombTime = 35;

		if (difficulty > 7)
			mysteryPosition = 13 - difficulty;

	}

	private void instantiate() { // INSTANTIATE

		wires = new Wire[numberOfWires];
		wireOrder = new int[numberOfWires];

		for (int i = 0; i < numberOfWires; i++) {
			int j = rand.nextInt(8);
			wires[i] = new Wire(i, j);
			wireOrder[i] = j;
		}

		for (int i = numberOfWires - 1; i >= 0; i--) { // SHUFFLE

			int index = rand.nextInt(i + 1); // 0-13
			int transport = wireOrder[index];
			wireOrder[index] = wireOrder[i];
			wireOrder[i] = transport;

		}// SHUFFLE

	}// INSTANTIATE

	public boolean cutWire(int cutX, int cutY) {

		for (int i = 0; i < numberOfWires; i++) {
			if (wires[i].cut == false) {
				if (cutX > wires[i].x && cutX < (wires[i].x + wires[i].width) && cutY > wires[i].y
						&& cutY < (wires[i].y + wires[i].height)) {

					if (wires[i].wireNumber == wireOrder[nextWireToCut]) { // correct
																			// wire
						wires[i].cut = true;
						nextWireToCut++;
						if (Settings.soundEnabled)
							Assets.bitten.play(1);
						return false;

					} else {// incorrect wire
						return true;
					}
				}

			}
		}
		return false; // nothing touched

	}
}
